package baidu.com.matedemo.dlp;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.baidu.duer.smartmate.proxy.bean.LocationMessage;
import com.baidu.duer.smartmate.proxy.controller.ControllerManager;
import com.baidu.duer.smartmate.proxy.controller.DCSDataObserver;

import baidu.com.matedemo.base.BaseFragment;
import baidu.com.matedemo.R;
import baidu.com.matedemo.DemoApp;
import baidu.com.matedemo.util.DemoUtils;

/**
 * Created by niufei01 on 2017/9/11.
 * <p>
 * 设备地理位置信息页
 */

public class LocationDemo extends BaseFragment implements DCSDataObserver<LocationMessage> {

    ControllerManager controllerManager;
    ViewHolder viewHolder;

    @Override
    public View onCreateContentViewCompat(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.location, viewGroup, false);
    }

    @Override
    public void onContentViewCreated(View view) {
        viewHolder = new ViewHolder(view);
        controllerManager = DemoApp.getInstance().getCurreDuerDevice().getControllerManager();
        controllerManager.registerLocationObserver(this);
        viewHolder.getstatus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                controllerManager.getLocationStatus();
            }
        });
        viewHolder.setlocation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 北京经纬度
                controllerManager.setLocation(116.4136103013d, 39.9110666857d, LocationMessage.GeoCoordinateSystem.WGS84);
            }
        });
    }

    @Override
    public void onDataChanaged(String event, LocationMessage message) {
        viewHolder.edittext.setText(message != null ? DemoUtils.toFormatJson(message) : "数据为空");
    }

    public class ViewHolder {
        public View rootView;
        public EditText edittext;
        public Button getstatus;
        public Button setlocation;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.edittext = (EditText) rootView.findViewById(R.id.edittext);
            this.getstatus = (Button) rootView.findViewById(R.id.getstatus);
            this.setlocation = (Button) rootView.findViewById(R.id.setlocation);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (controllerManager != null) {
            controllerManager.unregisterLocationObserver(this);
        }
    }
}
